package com.wokeMy.view.channe.huixiang;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class HuixiangRZActivity extends BaseActivity implements View.OnClickListener{
    ImageView backz_hxrz_iv;
    RelativeLayout jscard_hxrz_rl,jsname_hxrz_rl;
    Button zhifu_hxrz_bt;
    TextView jscard_hxrz_tv,jsname_hxrz_tv;
    EditText lhh_hxrz_tv;
    Data_bindcarlist listdata;
    UnionPayChannel unionPayChannel;
    String pay_card_no,bankCode,controller;
    String user_id;
    int way_id;
    String thirdPayment;
    int payWay;
    String useName,merchantName,merchantBillName,legalPersonID,merchantPersonPhone,bankName;

    ArrayList<String> options1Items= new ArrayList<String>();
    OptionsPickerView pvOptions;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_huixiang_rz);
    }

    @Override
    public void initActivity() {
        backz_hxrz_iv.setOnClickListener(this);
        jscard_hxrz_rl.setOnClickListener(this);
        jsname_hxrz_rl.setOnClickListener(this);
        zhifu_hxrz_bt.setOnClickListener(this);

    }

    @Override
    public void initView() {
        backz_hxrz_iv = myFindViewById(R.id.backz_hxrz_iv);
        jscard_hxrz_rl = myFindViewById(R.id.jscard_hxrz_rl);
        jscard_hxrz_tv = myFindViewById(R.id.jscard_hxrz_tv);
        jsname_hxrz_rl = myFindViewById(R.id.jsname_hxrz_rl);
        jsname_hxrz_tv = myFindViewById(R.id.jsname_hxrz_tv);
        zhifu_hxrz_bt = myFindViewById(R.id.zhifu_hxrz_bt);
        lhh_hxrz_tv = myFindViewById(R.id.lhh_hxrz_tv);


    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Intent intent=getIntent();
        Datas_load datas_load=myApp.getDatas_load();
        Log.e("legalPersonName",datas_load.toString());
        user_id=datas_load.getId();
        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        Log.e("unionPayChannel",unionPayChannel.toString());
        way_id=unionPayChannel.getWay();
        thirdPayment =unionPayChannel.getThirdPayment();
        payWay=unionPayChannel.getKind();
        controller=unionPayChannel.getController();
        String name="名字为空";
        if (!datas_load.getUser_nicename().equals("")){
            name=datas_load.getUser_nicename();
        }else {
            name=datas_load.getCom_name();
        }
        useName=name;
        merchantName=name+"的商户";
        merchantBillName=name+"的商户";
        legalPersonID=datas_load.getUser_no();
//        merchantPersonPhone=datas_load.getUser_login();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backz_hxrz_iv:
                finish();
                break;
            case R.id.jscard_hxrz_rl:
                Intent intent = new Intent (HuixiangRZActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "huixiangrz");
                startActivityForResult (intent, Constant.REQUEST_HXRZ);
                break;
            case R.id.jsname_hxrz_rl:
                hqBankName();
//                Intent intent2 = new Intent (HuixiangRZActivity.this, MybankblindActivity.class);
//                intent2.putExtra ("czhiortxian", "rhlz");
//                startActivityForResult (intent2, Constant.REQUEST_RHLZ);
                break;
            case R.id.zhifu_hxrz_bt:
                    bankCode=lhh_hxrz_tv.getText().toString().trim();
                    if (pay_card_no==null||pay_card_no.equals("")){
                        Toast.makeText(this,"请选择开户卡",Toast.LENGTH_SHORT).show();
                    }else if(bankName==null||bankName.equals("")) {
                        Toast.makeText(this,"请选择银行卡名称",Toast.LENGTH_SHORT).show();
                    }else if(bankCode==null||bankCode.equals("")) {
                        Toast.makeText(this,"请填写联行号",Toast.LENGTH_SHORT).show();
                    }else{
                        if (!fastClick()){
                            hxiangRzhu();
                        }
                    }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Constant.RESPONSE_HXRZ){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            merchantPersonPhone=listdata.phone;
            jscard_hxrz_tv.setText(pay_card_no);
        }
    }
    //汇享银行卡总行名称
    public void hqBankName(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        Log.e(" 汇享银行卡总行名称params","无"+params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.HXRZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("汇享银行卡总行名称", result);

                    JSONArray jsonArray = new JSONArray(result);
                    for (int j = 0; j < jsonArray.length(); j++) {
                        Log.e("aaaa",jsonArray.get(j)+" ");
                        options1Items.add(jsonArray.get(j)+"");
                    }
                    citySelect();
//                    try {
//                        JSONObject  jsonObject =new JSONObject(result);
//                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
//                        String respcode=jsonObject1.getString("respcode");
//                        if (respcode.equals("0000")){
//                            Toast.makeText(HuixiangRZActivity.this,"入驻成功",Toast.LENGTH_SHORT).show();
//                            finish();
//                        }else if(respcode.equals("2015")){
//                            Toast.makeText(HuixiangRZActivity.this,"商户已经入驻过",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(HuixiangRZActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
    //汇享商户开通入驻
    public  void  hxiangRzhu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",pay_card_no);
        params.put("controller",controller);
        params.put("merchant_name",merchantName);
        params.put("merchant_province","福建省");
        params.put("merchant_city","厦门市");
        params.put("merchant_address","福建省厦门市思明区虎园路2号");
        params.put("family_name",useName);
        params.put("id_card",legalPersonID);
        params.put("mobile",merchantPersonPhone); // 收款卡预留手机号码
        params.put("payee_bank_name",bankName);
        params.put("payee_branch_name",bankName);
        params.put("payee_branch_code",bankCode);
        params.put("payee_bank_province","福建省");
        params.put("payee_bank_city","厦门市");
        Log.e("//汇享商户开通入驻params","无"+params.toString());
        ahc.post(Constant.HXRZOPEN_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("汇享商户开通入驻", result);
                    JSONObject jsonObject =new JSONObject(result);
                    if (jsonObject.getString("flag").equals("success")) {
//                        if (null==jsonObject.getString("data")){
//                            Toast.makeText(HuixiangRZActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
//                              return;
//                        }
                        JSONObject jsonObject2=jsonObject.getJSONObject("data").getJSONObject("data");
                        String rhkjzfhtml =jsonObject2 .getString("resp_code");
                        if (rhkjzfhtml.equals("000000")){
                            Toast.makeText(HuixiangRZActivity.this,"入驻成功",Toast.LENGTH_LONG).show();
                            finish();

                        }else{
                            Toast.makeText(HuixiangRZActivity.this,jsonObject2.getString("resp_msg"),Toast.LENGTH_SHORT).show();

                        }

                    }else{
                        Toast.makeText(HuixiangRZActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
    public void citySelect() {
            pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    //返回的分别是三个级别的选中位置
                     bankName = options1Items.get(options1);
                    jsname_hxrz_tv.setText(bankName);
                }
            })
                    .setTitleText("银行选择")
                    .setContentTextSize(20)//设置滚轮文字大小
                    .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                    .setSelectOptions(0, 1)//默认选中项
                    .setBgColor(Color.WHITE)
                    .setTitleBgColor(Color.RED)
                    .setTitleColor(Color.WHITE)
                    .setCancelColor(Color.WHITE)
                    .setSubmitColor(Color.WHITE)
                    .setTextColorCenter(Color.LTGRAY)
                    .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                    .setLabels("省", "市", "区")
                    .setBackgroundId(0x66000000) //设置外部遮罩颜色
                    .build();
            pvOptions.show();
        pvOptions.setPicker(options1Items);//一级选择器*/

    }
}
